package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl extends ServiceImpl<ServiceItemMapper,ServiceItem> implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private BpmnInfoServiceImpl bpmnInfoService;

    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        QueryWrapper<ServiceItem> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(qo.getName()),"name",qo.getName());
        wrapper.eq(qo.getSaleStatus()!=null,"sale_status",qo.getSaleStatus());
        wrapper.eq(qo.getServiceCatalog()!=null,"service_catalog",qo.getServiceCatalog());
        wrapper.eq(qo.getAuditStatus()!=null,"audit_status",qo.getAuditStatus());
        wrapper.eq(qo.getCarPackage()!=null,"car_package",qo.getCarPackage());

        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectList(wrapper));
    }


    @Override
    public void updateServiceItem(ServiceItem serviceItem) {
        ServiceItem oldObj = this.getById(serviceItem.getId());

        //处于上架状态或审核中的商品不能修改
        if (ServiceItem.SALESTATUS_ON.equals(oldObj.getSaleStatus())) {
            throw new BusinessException("该商品处于上架状态，无法进行修改");
        } else if (ServiceItem.AUDITSTATUS_AUDITING.equals(oldObj.getAuditStatus())){
            throw new BusinessException("该商品正在审核中，无法进行修改");
        }
        //如果是审核通过，此时修改，需要将其状态变更为初始化
        if (ServiceItem.AUDITSTATUS_APPROVED.equals(oldObj.getAuditStatus())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        UpdateWrapper<ServiceItem> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",serviceItem.getId());
        wrapper.set("name",serviceItem.getName());
        wrapper.set("original_price",serviceItem.getOriginalPrice());
        wrapper.set("discount_price",serviceItem.getDiscountPrice());
        wrapper.set("info",serviceItem.getInfo());
        wrapper.set("service_catalog",serviceItem.getServiceCatalog());
        wrapper.set("audit_status",serviceItem.getAuditStatus());
        wrapper.set("sale_status",serviceItem.getSaleStatus());
        serviceItemMapper.update(null,wrapper);
    }


    @Override
    public List<ServiceItem> list() {
        QueryWrapper<ServiceItem> serviceItemQueryWrapper = new QueryWrapper<>();
        return serviceItemMapper.selectList(serviceItemQueryWrapper);
    }

    @Override
    public void saleOff(Long id) {
        //下架不需要进行任何业务上的判断则可以直接对上架状态进行修改

        UpdateWrapper<ServiceItem> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("sale_status",ServiceItem.SALESTATUS_OFF);
        serviceItemMapper.update(null,wrapper);
    }

    @Override
    public void saleOn(Long id) {
        ServiceItem oldObj = this.getById(id);
        //如果不是套餐，可以直接上架，如果是套餐，只有审核成功才能上架
        if (ServiceItem.CARPACKAGE_YES.equals(oldObj.getCarPackage()) &&
        !ServiceItem.AUDITSTATUS_APPROVED.equals(oldObj.getAuditStatus())) {

            throw new BusinessException("这是套餐需要审核通过才能上架,现在审核还没通过！");
        }
        UpdateWrapper<ServiceItem> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("sale_status",ServiceItem.SALESTATUS_ON);
        serviceItemMapper.update(null,wrapper);
    }

    @Override
    @Transactional
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        //根据传入的id查询服务项，服务项处于初始化状态才能进行审核操作
        ServiceItem serviceItem = this.getById(id);
        if ( id == null || !ServiceItem.AUDITSTATUS_INIT.equals(serviceItem.getSaleStatus())) {
            throw new BusinessException("非法操作！");
        }
        //修改服务状态，改为审核中
        UpdateWrapper<ServiceItem> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("sale_status",ServiceItem.AUDITSTATUS_AUDITING);
        serviceItemMapper.update(null,wrapper);
        //审核信息记录
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        carPackageAudit.setInfo(info);
        carPackageAudit.setCreateTime(new Date());
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        carPackageAudit.setServiceItemId(id);
        carPackageAudit.setServiceItemName(serviceItem.getName());
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        carPackageAudit.setAuditorId(director);
        carPackageAudit.setBpmnInfoId(bpmnInfoId);
        carPackageAuditService.save(carPackageAudit);

        //启动流程实例，以金额作为实例参数
        Map<String,Object> map = new HashMap<>();
        if (director != null) {
            map.put("director",director.toString());
        }
        if (finance != null) {
            map.put("finance",finance.toString());
        }
        map.put("discountPrice", serviceItem.getDiscountPrice().longValue());

        BpmnInfo bpmnInfo = bpmnInfoService.getById(bpmnInfoId);
        //启动流程
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey(bpmnInfo.getActProcessKey(), String.valueOf(carPackageAudit.getId()), map);
        //更新记录id
        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditService.updateById(carPackageAudit);

    }

    @Override
    public void changeAuditStatus(long id, Integer status) {
        UpdateWrapper<ServiceItem> serviceItemUpdateWrapper = new UpdateWrapper<>();
        serviceItemUpdateWrapper.eq("id",id);
        serviceItemUpdateWrapper.set("audit_status",status);
        serviceItemMapper.update(null, serviceItemUpdateWrapper);
    }

    @Override
    public void updateAuditStatus(Long id, Integer status) {
        UpdateWrapper<ServiceItem> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("sale_status",status);
        serviceItemMapper.update(null,wrapper);
    }


}
